<template>
  <div class="container">
    <navbar title="我的免单" theme="light"></navbar>
    <div>
      <div class="filterbar">
        <text @click="selectStatus" :class="['filterbar-item', searchParam.status === '' && 'filterbar-item-current']"
              status="">所有
        </text>
        <text @click="selectStatus"
              :class="['filterbar-item', searchParam.status === 'WAIT_VERIFY' && 'filterbar-item-current']"
              status="WAIT_VERIFY">待审核
        </text>
        <text @click="selectStatus"
              :class="['filterbar-item', searchParam.status === 'WAIT_RECEIVE_GOODS' && 'filterbar-item-current']"
              status="WAIT_RECEIVE_GOODS">待收货
        </text>
        <text @click="selectStatus"
              :class="['filterbar-item', searchParam.status === 'WAIT_SETTLE' && 'filterbar-item-current']"
              status="WAIT_SETTLE">待结算
        </text>
        <text @click="selectStatus"
              :class="['filterbar-item', searchParam.status === 'SETTLED' && 'filterbar-item-current']"
              status="SETTLED">已结算
        </text>
      </div>
    </div>
    <list
        class="order-list-container"
        @scroll="onPageScroll">
      <page-refresh v-on:refresh="onXRefresh" v-on:pullingDown="onPullingDown" :refreshing="refreshing"
                    :indicatorScale="indicatorScale"></page-refresh>
      <cell v-for="order in orderList" v-bind:key="order.id">
        <div class="order">
          <div class="order-item" @click="orderClick(order)">
            <image :src="order.itemPicUrl" class="order-item-pic"></image>
            <div class="order-item-detail">
              <text class="order-item-title">{{order.itemTitle}}</text>
              <text class="order-item-shop-title">卖家：{{order.itemShopTitle}}</text>
              <div class="flex-row space-between">
                <wxc-tag type="solid"
                         class="order-item-tag"
                         :tag-color="getStatusTagColor(order.status)"
                         font-color="#ffffff"
                         :value="order.statusTitle"></wxc-tag>
                <text class="order-activity-name" @click="goFreeChargeActivity(order.freeChargeActivityId)">{{order.freeChargeActivityName}}&gt;&gt;</text>
              </div>
              <div class="flex-row space-between">
                <wxc-rich-text
                    :config-list="[{type: 'text', value: '¥', style: yStyle}, {type: 'text', value: order.itemPrice.toFixed(2), style: valueStyle}]"></wxc-rich-text>
                <text class="updatetime-text">{{order.updateTime}}更新</text>
              </div>
            </div>
          </div>
        </div>
      </cell>
      <cell>
        <page-notice msg="抱歉，什么都木有找到～" v-if="!orderList.length && !hasNext"></page-notice>
      </cell>
      <page-loading-more v-on:loadingMore="onLoadingMore" :loadingMore="loadingMore"
                         :shown="hasNext"></page-loading-more>
    </list>
    <wxc-loading :show="loadingShow"></wxc-loading>
    <page-scrolltop :shown="pageScrollTopShown" @click="gotoTop"></page-scrolltop>
  </div>
</template>
<style lang="scss">
  @import "./sass/var";
  @import "./sass/mixin";
  @import "./sass/utilities";
  @import "./sass/icon";
  @import "./sass/buttons";

  .container {
    background-color: $body-bg;
    transition: transform .3s linear
  }

  .filterbar {
    height: 80px;
    line-height: 80px;
    flex-direction: row;
    /*justify-content: space-between;*/
    background-color: #fff;
    &-item {
      flex: 1;
      height: 80px;
      line-height: 80px;
      color: $color;
      font-size: 28px;
      padding: 0 20px;
      text-align: center;
      @include border-bottom(1, solid, #ddd);
      &-current {
        color: $color-primary;
        @include border-bottom(2px, solid, $brand-primary);
      }
    }
  }

  .updatetime-text {
    font-size: 20px;
    color: $color-muted;
  }

  .trade-no {
    &-row {
      flex-direction: row;
      justify-content: space-between;
      @include border-top(1px, solid, #ddd);
      padding-top: 20px;
    }
    &-text {
      font-size: 26px;
      color: $color;
    }
  }

  .order {
    margin-bottom: 10px;
    background-color: #ffffff;
    $padding: 20px;
    padding: $padding;
    $picHeight: 200px;
    $height: $picHeight + $padding * 2;
    &-activity-name {
      font-size: 28px;
      color: $color-purple;
      line-height: 28px;
      padding: 10px;
      height: 40px;
    }
    &-rebate {
      &-text {
        font-size: 30px;
        line-height: 30px;
        color: $color-warning;
      }
      &-title {
        font-size: 26px;
        line-height: 26px;
        color: $color;
      }
    }
    &-item {
      flex-direction: row;
      &-pic {
        width: $picHeight;
        height: $picHeight;
        border-radius: 5px;
      }
      &-detail {
        flex: 1;
        padding-left: 20px;
        justify-content: space-between;
      }
      &-title {
        font-size: 24px;
        line-height: 28px;
        color: $color;
        margin-bottom: 5px;
      }
      &-shop-title {
        font-size: 24px;
        color: $color-muted;
        margin-bottom: 5px;
      }
      &-tags {
        flex-direction: row;
      }
      &-tag {
        margin-right: 10px;
      }
    }
    &-create-time {
      color: $color-muted;
      font-size: 24px;
    }
    &-footer {
      background-color: #f6f6f6;
    }
  }

  .order-row {
    flex-direction: row;
    justify-content: center;
    padding: 20px 0 10px 0;
    &-cell {
      flex: 1;
    }
    &-title {
      color: $color-muted;
      font-size: 24px;
    }
    &-text {
      padding: 10px 5px;
    }
  }

  .remark {
    font-size: 24px;
    color: $color;
    background-color: $bg-warning;
    padding: 10px;
    margin-top: 20px;
    border-radius: 5px;
  }
</style>
<script>

import {WxcLoading, WxcRichText, WxcTag} from 'weex-ui'
let api = require('./include/api')
let Deferred = require('Deferred')
let navigator = require('./include/xnavigator')

let theme = require('./include/theme')
require('./include/page-xdom')
const DEFAULT_SEARCH_PARAM = {
  page: 1,
  status: ''
}
module.exports = {
  mixins: [
    require('./mixin/page-refresh'),
    require('./mixin/page-loading-more'),
    require('./mixin/page-scrolltop')
  ],
  components: {
    'page-notice': require('./components/page-notice.vue'),
    'navbar': require('./components/navbar.vue'),
    WxcTag,
    WxcRichText,
    WxcLoading
  },
  props: {
    height: {default: 0},
    back: {default: true}
  },
  data () {
    return {
      loaded: false,
      orderList: [],
      refreshing: false,
      refreshIndicatorScale: 0.2,
      hasNext: true,
      searchParam: Object.assign({}, DEFAULT_SEARCH_PARAM),
      loading: false,
      iosShow: false,
      loadingShow: false,
      yStyle: {
        fontSize: 20,
        color: theme.color
      },
      valueStyle: {
        fontSize: 32,
        color: theme.color
      }
    }
  },
  created () {
    Deferred.when(this.loadData(), this.search()).always(() => {
      this.loaded = true
    })
  },
  methods: {
    loadData () {

    },
    orderClick (order) {
      if (order) {
        navigator.push('commission-item.js?id=' + order.itemId + '&freeChargeActivityId=' + order.freeChargeActivityId)
      }
    },
    goFreeChargeActivity (id) {
      navigator.push('free-charge-activity.js?id=' + id)
    },
    resetSearchParam () {
      Object.assign(this.searchParam, DEFAULT_SEARCH_PARAM)
      this.hasNext = true
    },
    selectStatus (e) {
      this.resetSearchParam()
      this.searchParam.status = e.target.attr.status
      this.search()
    },
    getStatusTagColor (status) {
      switch (status) {
        case 'WAIT_VERIFY':
          return '#e39334'
        case 'WAIT_RECEIVE_GOODS':
          return '#5eb1de'
        case 'WAIT_SETTLE':
          return '#d9534f'
        case 'SETTLED':
          return '#5cb85c'
      }
    },
    search (append, callback) {
      this.loading = true
      return api.freeChargeActivityOrder.search(this.searchParam).done((rs) => {
        if (append) {
          this.orderList = this.orderList.concat(rs.data.results)
        } else {
          this.orderList = rs.data.results
        }
        let paging = rs.data
        this.hasNext = paging.hasNext
      }).always(() => {
        this.loading = false
        callback && callback()
      })
    },
    onXRefresh (event) {
      this.searchParam.page = 1
      this.onRefresh(event, Deferred.when(this.loadData(), this.search()))
    },
    onLoadingMore (event) {
      this.loadingMore = true
      this.searchParam.page = this.searchParam.page + 1
      this.search(true, () => {
        this.loadingMore = false
      })
    },
    gotoTop () {
      let dom = weex.requireModule('dom')
      dom.scrollToElement(this.$refs.orderList)
    },
    onPageScroll (event) {
      this.onPageScrollForScrollTop(event)
    }
  }
}
</script>
